/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import listo.client.actions.RedoAction;
import listo.client.actions.UndoAction;
import listo.client.model.Context;
import listo.client.model.operations.Operation;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Manages all updates to the status bar.
 */
@Singleton
public class StatusBarManager extends MouseAdapter implements ContextManager.ContextChangedListener {

    private JLabel messageLabel;
    private JLabel statusLabel;

    private Action currentAction;

    @Inject
    public StatusBarManager(ContextManager contextManager) {
        contextManager.addContextChangedListener(this);
    }

    public void setMessageLabel(JLabel messageLabel) {
        this.messageLabel = messageLabel;
    }

    public void setStatusLabel(JLabel statusLabel) {
        this.statusLabel = statusLabel;
    }

    public void setStatusText(String formatStr, Object... args) {
        setStatusText(String.format(formatStr, args));
    }

    public void setStatusText(String text) {
        statusLabel.setText(text);
    }

    public void mouseEntered(MouseEvent event) {
        if (event.getSource() instanceof AbstractButton) {
            AbstractButton button = (AbstractButton) event.getSource();
            currentAction = button.getAction();
            update();
        } else if (event.getSource() instanceof JMenuItem) {
            JMenuItem menuItem = (JMenuItem) event.getSource();
            currentAction = menuItem.getAction();
            update();
        }
    }

    public void mouseExited(MouseEvent event) {
        if (currentAction != null) {
            currentAction = null;
            update();
        }
    }

    // CAUTION: This method is being called from the context thread.
    public void currentContextChanged(Context context, Operation undoOp, Operation redoOp) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                setStatusText(" ");
                //setStatusText("%1$s tasks, %2$s folders, %3$s/%4$s ops", context.getTaskCount(), context.getFoldersCount(),
                //        undoOps, redoOps);
                if (currentAction instanceof UndoAction || currentAction instanceof RedoAction) {
                    update();
                }
            }
        });
    }

    private void update() {
        String text = currentAction != null ? (String) currentAction.getValue(Action.LONG_DESCRIPTION) : "";
        messageLabel.setText(text);
    }

}
